<template>
	<view class="login">
		<view style="height: 0; width: 100%; background: #bad8f2">
			<view class="status_bar">
				<view @tap="goHome">首页</view>
			</view>
		</view>
		<view class="logo_bkg">
			<view class="logo_content">
				<image src="./top_bkg.png" style="width: 100%" mode="widthFix"></image>
				<text class="login_title">
					<text class="login_sub_title">你好</text>
					欢迎登录/注册汇租机
				</text>
				<image src="./logo.png" style="width: 100%" mode="aspectFit" class="llsc"></image>
			</view>
		</view>
		<view class="login_top" v-if="isPhoneLoginH5">
			<view class="namePhone">
				<view class="name">
					<view class="name_phone">
						<text class="text">+86</text>
						<input style="font-size: 28rpx" v-model="phone" type="number" @input="userTelInput()"
							placeholder="请输入手机号码" />
					</view>
				</view>
				<view class="phone" style="margin: 40rpx 0;" v-if="randCodeImage">
					<view class="phone_number">
						<image class="icon" src="./image.png" mode="aspectFit"></image>
						<input style="font-size: 28rpx" v-model="imageYzm" maxlength="4" placeholder="请输入图形验证码" />
						<image :src="randCodeImage" mode="widthFix" @click="getImageYzm"
							style="width: 150rpx;margin-left: auto;">
						</image>
					</view>
				</view>
				<view class="phone" :style="{margin:!randCodeImage?'40rpx 0' : 0}">
					<view class="phone_number">
						<image class="icon" src="./psd.png" mode="aspectFit"></image>
						<input style="font-size: 28rpx" v-model="yzm" type="number" maxlength="6"
							placeholder="请输入手机验证码" />
						<text :style="{
				                backgroundColor: themeColor
				            }" class="yam_num" @click="get_code">
							{{ yzm_text }}
						</text>
					</view>
				</view>
				<text class="tips">未注册手机号验证后将自动注册</text>
				<view class="ysxy">
					<u-checkbox shape="circle" @change="checkboxChange" v-model="check_stu"
						active-color="#20b5ef">我同意本站</u-checkbox>
					<text @click="open_url1(1)" class="b">《注册协议》</text>
					和
					<text @click="open_url1(2)" class="b">《隐私协议》</text>
				</view>
			</view>

			<button class="sub_login" :style="{
                    backgroundColor: themeColor
                }" @click="sub_login">
				立即登录/注册
			</button>
		</view>

		<!-- <view>
			<view
				style="margin-top: 100rpx;display: flex;flex-direction: column;justify-content: center;align-items: center;">
				<view class="zfb_login" @click="zhifubaoLoginH5"
					style="letter-spacing: 0.1em;font-weight: 600;font-size: 32rpx;width: 90%;padding: 20rpx 50rpx;border-radius:60rpx;margin: 0 auto 20rpx auto;text-align: center;display: flex;align-items: center;justify-content: center;background-color: #e0f7fd;">
					<image src="../../static/zfb_icon.png" style="width: 60rpx;height: 60rpx;margin-right: 10rpx;"
						mode="aspectFit"></image>
					<text>支付宝授权登录</text>
				</view>
				<view class="ysxy" v-if="!isPhoneLoginH5">
					<u-checkbox shape="circle" @change="checkboxChange" v-model="check_stu"
						active-color="#f0945b">我同意本站</u-checkbox>
					<text @click="open_url1(1)" class="b">《注册协议》</text>
					和
					<text @click="open_url1(2)" class="b">《隐私协议》</text>
				</view>
				<view style="color: #000;font-weight: 600;">使用支付宝登录更加便捷更易通过！</view>
			</view>

			<view class="phone_login_h5" v-if="!isPhoneLoginH5" @click="phoneLoginH5"
				style="width: max-content;padding: 10rpx 50rpx;border-radius:60rpx;margin: 0 auto;text-align: center;display: flex;align-items: center;justify-content: center;background-color: #efefef;">
				<text>其他登录方式</text>
			</view>
		</view> -->




		<!-- 滑块验证组件 -->
		<sliderHandVue ref="sliderComponent" :show="isShowSliderVerify"
			@sliderVerifySuccessHandle="sliderVerifySuccessHandle" @sliderStaIsFalse="sliderStaIsFalse">
		</sliderHandVue>
	</view>
</template>
<script>
	const CryptoJS = require('crypto-js');
	import sliderHandVue from '../sliderHand/sliderHand.vue';
	import {
		request,
		getZWZUserDetail,
		checkHasUserBasicInfo,
		checkHasShiMingRenZheng,
		saveUserId,
		saveZWZUserDetail
	} from '@/utils';
	import {
		baseUrl
	} from '@/utils/constant';
	export default {
		data() {
			return {
				isShowSliderVerify: false,
				yzm_text: '获取验证码',
				yzm_text_ing: false,
				yzm_daojishi: 0,
				web_view: false,
				check_stu: false,
				phone: '',
				yzm: '',
				imageYzm: '',
				userDetail: '',
				codeKey: '',
				codeTime: '',
				appLogo: '',
				appName: '',
				randCodeImage: '', //图片验证码
				isPhoneLoginH5: true,
				appLoginAppidStr: '',
				isWxBrowser: false, //默认不是微信环境
			};
		},
		components: {
			sliderHandVue
		},
		onLoad() {
			this.judgeBrowserEnv()
			console.log('app_appid:=====' + this.APP_APPID);
			// this.getAppletConfigByAppid();
		},

		onShow() {
			if (this.$getUid() && getZWZUserDetail().token) {
				uni.switchTab({
					url: '/pages/index/index'
				});
			}
			this.getAppLoginAppid()
		},
		methods: {
			phoneLoginH5() {
				this.check_stu = false
				this.isPhoneLoginH5 = true
			},
			//h5上的支付宝登录
			zhifubaoLoginH5() {
				if (this.check_stu == false) {
					uni.showToast({
						icon: 'none',
						title: '请同意协议'
					});
					return false;
				}
				let backUrl = 'https://mobile.zuji.fun/'
				let alipayUrl =
					`https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=${this.appLoginAppidStr}&scope=auth_user&redirect_uri=` +
					encodeURIComponent(backUrl) + '&state=init';
				let openURL = 'alipays://platformapi/startapp?appId=20000067&url=' + encodeURIComponent(alipayUrl);
				if (this.isWxBrowser) {
					//微信环境
					let weixinUrl = `https://ds.alipay.com/?scheme=${encodeURIComponent(openURL)}`
					window.location.href = weixinUrl
				} else {
					//非微信环境（浏览器环境）
					window.location.href = openURL
				}
			},

			//app支付宝登录获取appid传给支付宝
			getAppLoginAppid() {
				this.$request('member/mall/aliPay/user/getAppPlatFormAppId', {}, 'get', false, true).then((
					res) => {
					if (res.businessSuccess || res.success) {
						this.appLoginAppidStr = res.data
					}
				});
			},
			// //判断当前浏览器环境
			judgeBrowserEnv() {
				let ua = navigator.userAgent.toLowerCase(); // 这里将所有字母转小写
				// let isWxBrowser = ua.indexOf('micromessenger') !== -1 //微信环境
				if (ua.indexOf('micromessenger') !== -1) {
					this.isWxBrowser = true
				} else {
					this.isWxBrowser = false
				}
			},
			checkboxChange(check) {
				this.check_stu = check;
			},



			sliderStaIsFalse(val) {
				console.log('要关闭弹窗吗', val);
				this.isShowSliderVerify = val
			},
			sliderVerifySuccessHandle(val) {
				console.log('回传到父组件的数据', val.left, val.sleft);
				this.getVerificationCode(val.left.toString(), val.sleft.toString())
			},
			//滑块数据后端验证传输数据进行加密
			slideDataRencryption(content) {
				// 密钥
				const secretKey = 'FA08438E01A27F2EE9B7B48D25E34389';
				// 初始向量（IV），必须为128位或256位
				const iv = '3c28c1bef9e73000';
				// let content = '123'
				return CryptoJS.AES.encrypt(content, CryptoJS.enc.Utf8.parse(secretKey), {
					iv: CryptoJS.enc.Utf8.parse(iv),
					mode: CryptoJS.mode.CBC,
					padding: CryptoJS.pad.Pkcs7
				}).toString();
			},
			//新滑块验证获取手机验证码
			getVerificationCode(left, sleft) {
				var that = this;
				let newLeft = that.slideDataRencryption(left)
				let newSleft = that.slideDataRencryption(sleft)
				console.log('传到父组件的连个left', newLeft, newSleft);
				that.$request(
					'/member/mall/aliPay/user/sendSmsCodeAndCheck', {
						mobile: that.phone,
						left: newLeft,
						sleft: newSleft
					},
					'GET',
					false
				).then((res) => {
					if (res.success || res.businessSuccess) {
						that.isShowSliderVerify = false
						that.codeKey = res.data.codeKey;
						that.codeTime = res.data.codeTime;
						uni.showToast({
							icon: 'none',
							title: '验证码发送成功，请注意查收!',
							duration: 2000
						});
						that.yzm_text_ing = true;
						that.yzm_daojishi = 120; //倒计时120秒
						that.yzm_text = that.yzm_daojishi + 'S';
						that.setInterval = setInterval(function() {
							that.yzm_daojishi = that.yzm_daojishi - 1; //时间减少
							that.yzm_text = that.yzm_daojishi + 'S';
							if (that.yzm_daojishi <= 0) {
								that.yzm_text = '重新获取';
								that.yzm_daojishi = 0;
								that.yzm_text_ing = false;
								clearInterval(that.setInterval);
							}
						}, 1000);
					} else {
						that.$refs.sliderComponent.refresh()
						uni.showToast({
							icon: 'none',
							title: res.msg,
							duration: 1000
						});
					}
				})
			},
			//监测手机号的输入（输入11位后出现图片验证码）
			userTelInput() {
				console.log('--监测手机号的输入--', this.phone);
				if (this.phone.length == 11) {
					// this.getImageYzm()
				}
			},
			//在输入手机号之后，获取图片验证码
			getImageYzm() {
				if (this.phone.length != 11) {
					uni.showToast({
						icon: 'none',
						title: '手机号输入错误！'
					});
					return false;
				}
				this.$request(`member/mall/aliPay/user/randomImage`, {
					mobile: this.phone
				}, 'get', false, true).then((
					res) => {
					if (res.businessSuccess || res.success) {
						this.randCodeImage = res.data;
					}
				});
			},

			open_url1(type) {
				this.$onTo(`pagesOther/xieyi/loginXy?type=` + type);
			},
			get_code() {
				var that = this;
				var myreg = /^(((1[0-9]{2}))+\d{8})$/;
				if (!myreg.test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: '手机号格式错误'
					});
					return false;
				}
				this.isShowSliderVerify = true
				return




				var that = this;
				var myreg = /^(((1[0-9]{2}))+\d{8})$/;
				if (!myreg.test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: '手机号格式错误'
					});
					return false;
				}
				// if (this.imageYzm.length != 4) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '图片验证码错误'
				// 	});
				// 	return false;
				// }
				if (that.yzm_text_ing === true) {
					//倒计时中
					return false;
				}
				that.yzm_text_ing = true;
				that.yzm_daojishi = 120; //倒计时30秒
				that.yzm_text = that.yzm_daojishi + 'S';
				request(
					'/member/mall/aliPay/user/sendSmsCode', {
						mobile: that.phone,
						captcha: that.imageYzm
					},
					'GET',
					false
				).then((res) => {
					console.log('发送短信====', res);
					if (res.businessSuccess || res.success) {
						that.codeKey = res.data.codeKey;
						that.codeTime = res.data.codeTime;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});

				that.setInterval = setInterval(function() {
					that.yzm_daojishi = that.yzm_daojishi - 1; //时间减少
					that.yzm_text = that.yzm_daojishi + 'S';

					if (that.yzm_daojishi <= 0) {
						that.yzm_text = '重新获取';
						that.yzm_daojishi = 0;
						that.yzm_text_ing = false;
						clearInterval(that.setInterval); //
					}
				}, 1000);
			},
			sub_login(e) {
				if (this.phone == '13991373115') {
					uni.showToast({
						icon: 'none',
						title: '登录出错'
					});
					return
				}
				var that = this;
				if (this.check_stu == false) {
					uni.showToast({
						icon: 'none',
						title: '请同意协议'
					});
					return false;
				}
				var myreg = /^(((1[0-9]{2}))+\d{8})$/;
				if (!myreg.test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: '手机号错误'
					});
					return false;
				}
				// if (this.imageYzm.length != 4) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '图片验证码错误'
				// 	});
				// 	return false;
				// }
				if (this.yzm.length != 6) {
					uni.showToast({
						icon: 'none',
						title: '手机验证码错误'
					});
					return false;
				}
				var postData = {};
				postData.phone = this.phone;
				postData.yzm = this.yzm;

				request(
					'member/mall/aliPay/user/smsLogin', {
						codeKey: that.codeKey,
						codeTime: that.codeTime,
						smsCode: that.yzm,
						telephone: that.phone,
						// captcha: that.imageYzm
					},
					'POST',
					false
				).then((res) => {
					if (res.code && res.code == '500') {
						//登陆失败
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
						return false;
					}
					if (res.data.errorCode == '-1') {
						uni.showToast({
							icon: 'none',
							title: res.data.errorMessage
						});
						return false;
					}
					that.userDetail = res.data;
					if (that.userDetail.uid) {
						saveUserId(that.userDetail.uid);
					}
					saveUserId(that.userDetail.uid);
					saveZWZUserDetail(that.userDetail);
					// uni.reLaunch({ //跳转到首页
					// 	url: "/pages/index/index"
					// })
					// uni.showToast({
					// 	icon: 'none',
					// 	title: '欢迎登录/注册RICH购'
					// });
					uni.navigateBack({
						delta: 1
					});
				});
			},
			getAppletConfigByAppid() {
				request(
					'member/mall/alipay/applet/getAppletConfigByAppid', {
						appid: this.APP_APPID
					},
					'GET',
					false
				).then((res) => {
					console.log('app信息', res);
					if (res.data.logo) {
						this.appLogo = res.data.logo;
					} else {
						this.appLogo = this.drawLogo(res.data.name ? res.data.name : 'app');
					}
					this.appName = res.data.name;
				});
			},
			goHome() {
				uni.switchTab({
					url: '/pages/index/index'
				});
			},
			checkboxChange(check) {
				this.check_stu = check;
			},
			drawLogo: function(text) {
				// 创建画布
				let canvas = document.createElement('canvas');
				// 绘制文字环境
				let context = canvas.getContext('2d');
				// 设置字体
				// context.font = "宋体";
				// 获取字体宽度
				// let width = context.measureText(text).width;
				// 如果宽度不够 240
				// if (width < 240) {
				//     width = 240;
				// } else {
				//     width = width + 30;
				// }
				// 画布宽度
				canvas.width = 50;
				// 画布高度
				canvas.height = 50;
				// 填充白色
				context.fillStyle = '#ffffff';
				// 绘制文字之前填充白色
				context.fillRect(0, 0, canvas.width, canvas.height);
				// 设置字体
				// context.font = font;
				// 设置水平对齐方式
				context.textAlign = 'center';
				// 设置垂直对齐方式
				context.textBaseline = 'middle';
				// 设置字体颜色
				context.fillStyle = '#000000';
				// 绘制文字（参数：要写的字，x坐标，y坐标）
				context.fillText(text, canvas.width / 2, canvas.height / 2);
				// 生成图片信息
				let dataUrl = canvas.toDataURL('image/png');
				// console.log("1111111111111",dataUrl)
				return dataUrl;
			}
		}
	};
</script>

<style lang="less" scoped>
	.ysxy {
		font-size: 24rpx;
		text-align: center;
		color: #999999;
		padding: 20rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;

		.b {
			color: #f0945b;
		}

		/deep/.u-checkbox__label {
			font-size: 24rpx;
			margin-right: 0;
			color: #999999;
		}

		/deep/.u-checkbox__icon-wrap {
			width: 26rpx !important;
			height: 26rpx !important;
		}
	}

	.phone_login_h5 {
		position: absolute;
		bottom: 200rpx;
		left: 50%;
		transform: translateX(-50%);
		font-size: 24rpx;
	}


	uni-app {
		margin: 0;
		padding: 0;
		background-color: #fff;
	}

	.login-text {
		font-size: 44rpx;
		color: black;
		font-weight: 700;
	}

	.logo_bkg {
		min-height: 300rpx;
		position: relative;
		height: auto;

		.logo_content {
			position: relative;
			text-align: center;
			left: 0;
			right: 0;
			font-size: 38rpx;
			font-family: 'MicrosoftYaHei';
			color: #fff;
			font-weight: bold;

			.llsc {
				position: absolute;
				top: 30%;
				left: 50%;
				width: 150rpx;
				height: 150rpx;
				transform: translateX(-50%);
			}

			.login_title {
				font-size: 48rpx;
				font-weight: bolder;
				text-align: left;
				padding: 0 0 0 80rpx;
				color: #000;
				width: 100%;
				display: block;
				width: 100%;

				.login_sub_title {
					display: block;
				}

				&::after {
					content: '';
					display: block;
					width: 80rpx;
					height: 10rpx;
					margin-top: 10rpx;
					border-radius: 10px;
					background-color: #1ab4f1;
				}
			}
		}
	}

	.login_top {
		display: flex;
		flex-direction: column;
		justify-content: center;
		box-sizing: border-box;
		align-items: center;
		padding: 0 20rpx 100rpx 20rpx;
		background-color: #fff;
		overflow: hidden;

		.namePhone {
			box-sizing: border-box;
			padding: 0 60rpx 20rpx;
			margin-top: 50rpx;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;
			width: 100%;

			/deep/.input-placeholder {
				font-size: 24rpx;
				color: #c5c5c5;
			}

			.name_icon {
				display: flex;
				align-items: center;
				color: #333;
				line-height: 28rpx;
				margin-bottom: 25rpx;
				font-size: 26rpx;
			}

			.name {
				display: flex;
				flex-direction: column;
				padding: 0 30rpx;
				border: 1px solid #f3f3f7;
				border-radius: 15rpx;

				.name_phone {
					height: 80rpx;
					line-height: 80rpx;
					display: flex;
					align-items: center;

					.text {
						display: inline-block;
						padding-right: 15rpx;
						margin-right: 15rpx;
						border-right: 1px solid #999;
						height: 28rpx;
						line-height: 28rpx;
					}
				}
			}

			.phone {
				display: flex;
				flex-direction: column;
				padding: 15rpx;
				border: 1px solid #f3f3f7;
				// margin: 40rpx 0;
				height: 80rpx;
				line-height: 80rpx;
				border-radius: 15rpx;

				.phone_number {
					display: flex;
					align-items: center;
					justify-content: flex-start;
					height: 80rpx;
					line-height: 80rpx;

					.icon {
						width: 40rpx !important;
						height: 40rpx !important;
						margin: 0 32rpx 0 18rpx;
					}
				}

				.yam_num {
					font-size: 22rpx;
					line-height: 45rpx;
					height: 45rpx;
					text-align: center;
					color: #fff;
					border-radius: 20rpx;
					padding: 0 20rpx;
					margin-left: auto;
					background-image: linear-gradient(180deg, #1ab4f1 0%, #1ab4f1 100%);
					box-shadow: 0 7rpx 28rpx 0 rgba(138, 114, 232, 0.3);
				}
			}

			.tips {
				text-align: center;
				font-size: 24rpx;
				margin: 10rpx 0;
				color: #999999;
			}

			.ysxy {
				font-size: 24rpx;
				text-align: center;
				color: #999999;
				padding: 20rpx 0;
				display: flex;
				justify-content: center;
				align-items: center;

				.b {
					color: #1ab4f1;
				}

				/deep/.u-checkbox__label {
					font-size: 24rpx;
					margin-right: 0;
					color: #999999;
				}

				/deep/.u-checkbox__icon-wrap {
					width: 26rpx !important;
					height: 26rpx !important;
				}
			}
		}

		.sub_login {
			width: 81%;
			color: #fff;
			font-weight: bold;
			letter-spacing: 0.05em;
			margin: 50rpx auto;
			font-size: 16px;
			border-radius: 10px;

			&::after {
				border: none;
			}

			background-image: linear-gradient(180deg, #1ab4f1 0%, #1ab4f1 100%);
			box-shadow: 0 7rpx 28rpx 0 rgba(138, 114, 232, 0.3);
		}
	}

	.status_bar {
		position: fixed;
		top: var(--status-bar-height);
		right: 20rpx;
		z-index: 999;
		font-size: 30rpx;
		font-weight: bold;
		font-family: 'Arial';
		padding: 20rpx;
		color: #000;
		font-weight: bold;
	}

	.lifestyle {
		position: absolute;
		bottom: 30rpx;
		left: 50%;
		transform: translateX(-50%);
	}
</style>